<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: SubscriberService.cfc 1048 2007-01-09 01:17:09Z pfarrell $
--->
<cfcomponent
	displayname="SubscriberService"
	output="false"
	hint="SubscriberService for MachBlog">
	
	<!---
	PROPERTIES
	--->
	<cfproperty name="subscriberGateway" type="SubscriberGateway" required="true" />
	<cfproperty name="subscriberDAO" type="SubscriberDAO" required="true" />
	<cfproperty name="userFacade" type="org.machblog.facades.UserFacade" required="true" />
	<cfproperty name="udfs" type="org.machblog.utils.udfs" required="true" />
	<cfproperty name="notificationService" type="org.machblog.notification.NotificationService" required="true" />
	
	<!---
	INITIALIZATION / CONFIGURATION
	--->
	<cffunction name="init" access="public" output="false" returntype="SubscriberService" 
		hint="Initializes the service.">
		<cfreturn this />
	</cffunction>
	
	<cffunction name="setSubscriberGateway" access="public" output="false" returntype="void">
		<cfargument name="subscriberGateway" type="SubscriberGateway" required="true" />
		<cfset variables.subscriberGateway = arguments.subscriberGateway />
	</cffunction>
	<cffunction name="getSubscriberGateway" access="public" output="false" returntype="SubscriberGateway">
		<cfreturn variables.subscriberGateway />
	</cffunction>
	
	<cffunction name="setSubscriberDAO" access="public" output="false" returntype="void">
		<cfargument name="subscriberDAO" type="SubscriberDAO" required="true" />
		<cfset variables.subscriberDAO = arguments.subscriberDAO />
	</cffunction>
	<cffunction name="getSubscriberDAO" access="public" output="false" returntype="SubscriberDAO">
		<cfreturn variables.subscriberDAO />
	</cffunction>
	
	<cffunction name="setUserFacade" access="public" output="false" returntype="void">
		<cfargument name="userFacade" type="org.machblog.facades.UserFacade" required="true" />
		<cfset variables.userFacade = arguments.userFacade />
	</cffunction>
	<cffunction name="getUserFacade" access="public" output="false" returntype="org.machblog.facades.UserFacade">
		<cfreturn variables.userFacade />
	</cffunction>
	
	<cffunction name="setUdfs" access="public" output="false" returntype="void">
		<cfargument name="udfs" type="org.machblog.utils.udfs" required="true" />
		<cfset variables.udfs = arguments.udfs />
	</cffunction>
	<cffunction name="getUdfs" access="public" output="false" returntype="org.machblog.utils.udfs">
		<cfreturn variables.udfs />
	</cffunction>
	
	<cffunction name="setNotificationService" access="public" output="false" returntype="void">
		<cfargument name="notificationService" type="org.machblog.notification.NotificationService" required="true" />
		<cfset variables.notificationService = arguments.notificationService />
	</cffunction>
	<cffunction name="getNotificationService" access="public" output="false" returntype="org.machblog.notification.NotificationService">
		<cfreturn variables.notificationService />
	</cffunction>
	
	<cffunction name="getSubscriberBean" access="public" output="false" returntype="Subscriber">
		<cfreturn createObject("component", "Subscriber").init() />
	</cffunction>
	
	<!---
	PUBLIC FUNCTIONS
	--->
	<cffunction name="getSubscribers" access="public" output="false" returntype="query" 
			hint="Returns a query object containing subscribers, optionally based on the search terms passed in">
		<cfargument name="searchText" type="string" required="false" default="" />
		<cfargument name="activeOnly" type="boolean" required="false" default="false" />
		
		<cfreturn getSubscriberGateway().getSubscribers(arguments.searchText, arguments.activeOnly) />
	</cffunction>
	
	<cffunction name="getSubscriptionCount" access="public" output="false" returntype="numeric" 
			hint="Returns the current number of subscribers to the blog">
		<cfreturn getSubscriberGateway().getSubscriptionCount() />
	</cffunction>
	
	<cffunction name="createSubscriber" access="public" output="false" returntype="void" 
			hint="Creates a subscriber">
		<cfargument name="subscriber" type="Subscriber" required="true" />
		
		<cfset arguments.subscriber.setEmail(trim(arguments.subscriber.getEmail())) />
		
		<cfif NOT getUdfs().isEmailAddress(arguments.subscriber.getEmail())>
			<cfthrow type="application" message="invalid email" detail="invalid email" />
		<cfelse>
			<cfset arguments.subscriber.setSubscriberId(createUUID()) />
			<cfif getUserFacade().userExists() AND getUserFacade().getUser().getUserId() NEQ "">
				<cfset arguments.subscriber.getAudit().setCreatedById(getUserFacade().getUser().getUserId()) />
			</cfif>
			
			<cfset getSubscriberDAO().create(arguments.subscriber) />
		</cfif>
	</cffunction>
	
	<cffunction name="getSubscriber" access="public" output="false" returntype="void" 
			hint="Retrieves subscriber details using the ID of the subscriber bean passed in">
		<cfargument name="subscriber" type="Subscriber" required="true" />
		
		<cfset getSubscriberDAO().read(arguments.subscriber) />
	</cffunction>
	
	<cffunction name="updateSubscriber" access="public" output="false" returntype="void" 
			hint="Updates subscriber details">
		<cfargument name="subscriber" type="Subscriber" required="true" />
		
		<cfset arguments.subscriber.setEmail(trim(arguments.subscriber.getEmail())) />
		
		<cfif NOT getUdfs().isEmailAddress(arguments.subscriber.getEmail())>
			<cfthrow type="application" message="invalid email" detail="invalid email" />
		<cfelse>
			<cfset arguments.subscriber.getAudit().setModifiedById(getUserFacade().getUser().getUserId()) />
			<cfset getSubscriberDAO().update(arguments.subscriber) />
		</cfif>
	</cffunction>
	
	<cffunction name="deleteSubscriber" access="public" output="false" returntype="void" 
			hint="Deletes a subscriber">
		<cfargument name="subscriber" type="Subscriber" required="true" />
		
		<cfset getSubscriberDAO().delete(arguments.subscriber) />
	</cffunction>
	
	<cffunction name="verifyBlogSubscription" access="public" output="false" returntype="boolean" 
			hint="Verifies a blog subscription using the email and verify id passed in">
		<cfargument name="email" type="string" required="true" />
		<cfargument name="verifyId" type="uuid" required="true" />
		
		<cfreturn getSubscriberGateway().verifyBlogSubscription(arguments.email, arguments.verifyId) />
	</cffunction>
	
	<cffunction name="emailSubscribers" access="public" output="false" returntype="void" 
			hint="Emails blog subscribers">
		<cfargument name="fromEmail" type="string" required="true" />
		<cfargument name="subject" type="string" required="true" />
		<cfargument name="body" type="string" required="true" />
		
		<!--- get the active subscribers --->
		<cfset var subscribers = getSubscribers(activeOnly = true) />
		
		<cfset getNotificationService().emailSubscribers(arguments.fromEmail, arguments.subject, arguments.body, subscribers) />
	</cffunction>
	
</cfcomponent>